﻿#include "dboperator.h"
#include "common/common.h"

#include <QMutexLocker>
#include <QDateTime>

DBOperator::DBOperator(QObject *parent)
    : SqliteDataProvider(parent)
{

}

DBOperator::~DBOperator()
{

}

/**
 * @brief DBOperator::getUserDataById 根据用户ID得到用户数据
 * @param userId
 * @param pUserData
 * @param connectionName
 * @param longConnect
 * @return
 */
bool DBOperator::getUserDataById(int userId,QString password,tagDBUserData *pUserData,
                     QString connectionName,
                     bool longConnect)
{
    if(userId <= 0 || password.isEmpty() || pUserData == NULL)
        return false;

    QMutexLocker locker(&m_mutexhtmldataprovider);

    QString sqlStr = QString("select * from g_user where id=%1 and password='%2'").arg(
                            QString::asprintf("%d",userId),
                            password);

    /// 执行SQL语句
    RecordSetList pRecordSetList = execSql(sqlStr,connectionName,longConnect);
    if(pRecordSetList.isEmpty())
        return false;

    pUserData->id = pRecordSetList(0)(0,0).toInt();
    pUserData->type = pRecordSetList(0)(0,1).toInt();
    pUserData->username = pRecordSetList(0)(0,2);
    pUserData->password = pRecordSetList(0)(0,3);
    pUserData->curmap = pRecordSetList(0)(0,4).toInt();
    pUserData->curmappos = convertStringToPoint(pRecordSetList(0)(0,5));
    pUserData->blood = pRecordSetList(0)(0,6).toInt();
    pUserData->defense = pRecordSetList(0)(0,7).toInt();
    pUserData->experience = pRecordSetList(0)(0,8).toInt();
    pUserData->grade = pRecordSetList(0)(0,9).toInt();
    pUserData->shapeinfo = pRecordSetList(0)(0,10);
    pUserData->bulletquantity = pRecordSetList(0)(0,11);
    pUserData->currentusingbullet = pRecordSetList(0)(0,12).toInt();
    pUserData->lastip = pRecordSetList(0)(0,13);
    pUserData->lasttime = pRecordSetList(0)(0,14).toLongLong();

    return true;
}

/**
 * @brief DBOperator::getUserData 得到指定名称和密码的用户信息
 * @param username
 * @param userpwd
 * @param pUserData
 * @return
 */
bool DBOperator::getUserData(QString username,QString userpwd,tagDBUserData *pUserData,
                            QString connectionName,
                            bool longConnect)
{
    if(username.isEmpty() || userpwd.isEmpty() || pUserData == NULL)
        return false;

    QMutexLocker locker(&m_mutexhtmldataprovider);

    QString sqlStr = QString("select * from g_user where username='%1' and password='%2'").arg(username,
                                                                                             userpwd);

    /// 执行SQL语句
    RecordSetList pRecordSetList = execSql(sqlStr,connectionName,longConnect);
    if(pRecordSetList.isEmpty())
        return false;

    pUserData->id = pRecordSetList(0)(0,0).toInt();
    pUserData->type = pRecordSetList(0)(0,1).toInt();
    pUserData->username = pRecordSetList(0)(0,2);
    pUserData->password = pRecordSetList(0)(0,3);
    pUserData->curmap = pRecordSetList(0)(0,4).toInt();
    pUserData->curmappos = convertStringToPoint(pRecordSetList(0)(0,5));
    pUserData->blood = pRecordSetList(0)(0,6).toInt();
    pUserData->defense = pRecordSetList(0)(0,7).toInt();
    pUserData->experience = pRecordSetList(0)(0,8).toInt();
    pUserData->grade = pRecordSetList(0)(0,9).toInt();
    pUserData->shapeinfo = pRecordSetList(0)(0,10);
    pUserData->bulletquantity = pRecordSetList(0)(0,11);
    pUserData->currentusingbullet = pRecordSetList(0)(0,12).toInt();
    pUserData->lastip = pRecordSetList(0)(0,13);
    pUserData->lasttime = pRecordSetList(0)(0,14).toLongLong();

    return true;
}

/**
 * @brief DBOperator::updateUserData 更新用户信息
 * @param pUserData
 * @param connectionName
 * @param longConnect
 * @return
 */
bool DBOperator::updateUserData(tagDBUserData *pUserData,
                    QString connectionName,
                    bool longConnect)
{
    if(pUserData == NULL)
        return false;

    QMutexLocker locker(&m_mutexhtmldataprovider);

    QString sqlStr = QString("update g_user set username='%1',password='%2',"
                             "type=%3,lastip='%4',lasttime=%5,curmappos='%6'"
                             " where id=%7").
            arg(pUserData->username,
                pUserData->password,
                QString::asprintf("%d",pUserData->type),
                pUserData->lastip,
                QString::asprintf("%lld",QDateTime::currentSecsSinceEpoch()),
                QString::asprintf("%d,%d",pUserData->curmappos.x(),pUserData->curmappos.y()),
                QString::asprintf("%ld",pUserData->id));

    /// 执行SQL语句
    execSql(sqlStr,connectionName,longConnect);

    return true;
}

/**
 * @brief DBOperator::updateUserMapData 更新用户地图信息
 * @param BaseRole
 * @param connectionName
 * @param longConnect
 * @return
 */
bool DBOperator::updateUserMapData(BaseRole *pBaseRole,QString connectionName,bool longConnect)
{
    if(pBaseRole == NULL)
        return false;

    QMutexLocker locker(&m_mutexhtmldataprovider);

    QString sqlStr = QString("update g_user set curmap=%1,curmappos='%2' where id=%3").
            arg(QString::asprintf("%d",pBaseRole->getMapIndex()),
                QString::asprintf("%d,%d",pBaseRole->getPosition().x(),pBaseRole->getPosition().y()),
                QString::asprintf("%d",pBaseRole->getID()));

    /// 执行SQL语句
    execSql(sqlStr,connectionName,longConnect);

    return true;
}

/**
 * @brief DBOperator::updateUserGameData 更新用户游戏数据信息
 * @param pBaseRole
 * @param connectionName
 * @param longConnect
 * @return
 */
bool DBOperator::updateUserGameData(BaseRole *pBaseRole,
                        QString connectionName,
                        bool longConnect)
{
    if(pBaseRole == NULL)
        return false;

    QMutexLocker locker(&m_mutexhtmldataprovider);

    QJsonArray bulletArr;

    QMap<int,int> tmpBulletQuantity = pBaseRole->getBulletQuantity();
    QMap<int,int>::iterator iter = tmpBulletQuantity.begin();
    for(;iter != tmpBulletQuantity.end();++iter)
    {
        QJsonObject bulletObj;
        bulletObj["id"] = iter.key();
        bulletObj["quantity"] = iter.value();

        bulletArr.push_back(bulletObj);
    }

    QString sqlStr = QString("update g_user set blood=%1,defense=%2,experience=%3,"
                             "grade=%4,bullets='%5',currentbullet=%6 where id=%7").
            arg(QString::asprintf("%d",pBaseRole->getBlood()),
                QString::asprintf("%d",pBaseRole->getDefense()),
                QString::asprintf("%d",pBaseRole->getExperience()),
                QString::asprintf("%d",pBaseRole->getGrade()),
                JsonArrayToString(bulletArr),
                QString::asprintf("%d",pBaseRole->getUsingBullet()),
                QString::asprintf("%d",pBaseRole->getID()));

    /// 执行SQL语句
    execSql(sqlStr,connectionName,longConnect);

    return true;
}
